package com.yicong.taxi.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.Display;
import android.widget.ImageView;

public class PictureUtils {
    /* 获取适当尺寸的图片大小，path为图片文字路径 */
    public static BitmapDrawable getScaledDrawble(Activity a, String path){
        Display display = a.getWindowManager().getDefaultDisplay();
        float desWidth = display.getWidth();
        float desHeight = display.getHeight();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;

        int scaleSize = 1;
        if(srcWidth > desWidth || srcHeight > desHeight){
            if(srcWidth > srcHeight)
                scaleSize = Math.round(srcHeight/desHeight);
            else
                scaleSize = Math.round(srcWidth/desWidth);
        }

        options = new BitmapFactory.Options();
        options.inSampleSize = scaleSize;

        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        return new BitmapDrawable(a.getResources(), bitmap);
    }

    /* 回收ImageView对象中图像数据 */
    public static void cleanImageView(ImageView imageView){
        if(!(imageView.getDrawable() instanceof BitmapDrawable))
            return;

        BitmapDrawable b = (BitmapDrawable) imageView.getDrawable();
        b.getBitmap().recycle();
        imageView.setImageDrawable(null);
    }
}
